﻿using System.Linq;
using good1.ApplicationServices.Interface;
using good1.Core;
using SharpArch.Core;
using SharpArch.Core.PersistenceSupport;

namespace good1.ApplicationServices
{
    public class LocationService : ILocationService<Location>
    {
        public LocationService(IRepository<Location> locationRepository, IRepository<Country> countryRepository)
        {
            Check.Require(locationRepository != null, "locationRepository may not be null");
            Check.Require(countryRepository != null, "countryRepository may not be null");
            this.locationRepository = locationRepository;
            this.countryRepository = countryRepository;
        }

        public Location Get(int id)
        {
            Location location =  locationRepository.Get(id);
            location.Countries = countryRepository.GetAll();
            return location;
        }

        public void SaveOrUpdate(Location location)
        {
            locationRepository.SaveOrUpdate(location);
        }

        public string Delete(int id)
        {
            string resultMessage = "The project was successfully deleted.";
            Location projectToDelete = locationRepository.Get(id);

            if (projectToDelete != null)
            {
                locationRepository.Delete(projectToDelete);

                try
                {
                    locationRepository.DbContext.CommitChanges();
                }
                catch
                {
                    resultMessage = "A problem was encountered preventing the location from being deleted. " +
                        "Another item likely depends on this location.";
                    locationRepository.DbContext.RollbackTransaction();
                }
            }
            else
            {
                resultMessage = "The project could not be found for deletion. It may already have been deleted.";
            }
            return resultMessage;
        }

        public void RollbackTransaction()
        {
            locationRepository.DbContext.RollbackTransaction();
        }

        public Location GetCreation()
        {
            Location loc = new Location();
            loc.Countries = countryRepository.GetAll();
            return loc;
        }

        public IQueryable<Location> GetAllSorted()
        {
            IQueryable<Location> locations = locationRepository.GetAll().AsQueryable();
            return locations.OrderBy(i => i.Country.CountryName);
        }

        public Country GetCountry(int id)
        {
            return countryRepository.Get(id);
        }

        public Map GetMapsByLocationId(int id)
        {
            Location location = locationRepository.Get(id);

            return new Map
            {
                Name = location.Place,
                Zoom = 7,
                LatLng = new LatLng { Latitude = (location.LatLng != null) ? location.LatLng.Latitude :0, Longitude = (location.LatLng != null) ? location.LatLng.Longitude: 0, },
                Locations =
                           {
                               new Location
                               {
                                   Place =  location.Place,
                                   LatLng = new LatLng { Latitude = (location.LatLng != null) ? location.LatLng.Latitude: 0, Longitude = (location.LatLng != null) ? location.LatLng.Longitude: 0 },
                                   Image = ""
                               }
                           }
            };
        }


        private readonly IRepository<Location> locationRepository;
        private readonly IRepository<Country> countryRepository;
    }
}
